import store from '../store/store.js';
import * as ActionTypes from '../constants/constants.js';
import { isUser, isWxUser, path, saveData, loadData, removeData, requstPromise } from '../util/utils.js';
import request from '../api/request.js';

// antd 按需加载
import { Toast } from '@ant-design/react-native';

// 轮播图列表
export async function rotationChart(options) {
  const { dispatch } = store
  const res = await request.rotationChart(options)
  return requstPromise(res, () => {

  })
}

// 发布商品评价
export async function goodsAppraise(options) {
  const { dispatch } = store
  const res = await request.goodsAppraise(options)
  return requstPromise(res, () => {

  })
}

// 发布商品评价的评论
export async function goodsAppraiseComment(options) {
  const { dispatch } = store
  const res = await request.goodsAppraiseComment(options)
  return requstPromise(res, () => {

  })
}

// 商品评价评论列表
export async function goodsAppraiseCommentList(options) {
  const { dispatch } = store
  const res = await request.goodsAppraiseCommentList(options)
  return requstPromise(res, () => {

  })
}

// 商品评价列表
export async function goodsAppraiseList(options) {
  const { dispatch } = store
  const res = await request.goodsAppraiseList(options)
  return requstPromise(res, () => {

  })
}

// 商品属性/ 规格
export async function goodsAttribute(options) {
  const { dispatch } = store
  const res = await request.goodsAttribute(options)
  return requstPromise(res, () => {

  })
}

// 商品收藏
export async function goodsCollection(options) {
  const { dispatch } = store
  const res = await request.goodsCollection(options)
  return requstPromise(res, () => {
    Toast.success(res.message, 1)
  })
}

// 商品收藏列表
export async function goodsCollectionList(options) {
  const { dispatch } = store
  const res = await request.goodsCollectionList(options)
  return requstPromise(res, () => {

  })
}

// 商品详情
export async function goodsDetail(options) {
  const { dispatch } = store
  const res = await request.goodsDetail(options)
  return requstPromise(res, () => {

  })
}

// 已注册用户绑定微信
export async function bindLogin(options) {
  const { dispatch } = store
  const res = await request.bindLogin(options)
  return requstPromise(res, () => {

  })
}

// 商品详情
export async function checkBindWechat(options) {
  const { dispatch } = store
  const res = await request.checkBindWechat(options)
  return requstPromise(res, () => {

  })
}


// 邀请登录 /invitation/login
export async function invitationLogin(options) {
  const { dispatch } = store
  const res = await request.invitationLogin(options)
  console.log('邀请码登录', res)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setLoginInfo,
      data: { ...options, ...res.data },
    });
    dispatch({
      type: ActionTypes.setToken,
      data: res.data.token
    })
  })
}

// 密码登录 /login
export async function login(options) {
  const { dispatch } = store
  const res = await request.login(options)
  console.log('登录', res)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setLoginInfo,
      data: { ...options, ...res.data },
    });
    dispatch({
      type: ActionTypes.setToken,
      data: res.data.token
    })
  })
}

// 短信验证码登录 /sms/login
export async function smsLogin(options) {
  const { dispatch } = store

  const res = await request.smsLogin(options)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setLoginInfo,
      data: { ...options, ...res.data },
    });
    dispatch({
      type: ActionTypes.setToken,
      data: res.data.token
    })
  })
}

// 微信登录 /sms/login
export async function wechatLogin(options) {
  const { dispatch } = store
  const res = await request.wechatLogin(options)
  return requstPromise(res, () => {
    // dispatch({
    //   type: ActionTypes.setLoginInfo,
    //   data: { ...options, ...res.data },
    // });
    // dispatch({
    //   type: ActionTypes.setToken,
    //   data: res.data.token
    // })
  })
}

// 发送短信验证码 /sms/send
export async function smsSend(options) {
  const { dispatch } = store
  const res = await request.smsSend(options)
  console.log(res, '发送验证')
  return requstPromise(res)
}

// 设置登录密码 /setting/password
export async function settingPassword(options) {
  const { dispatch } = store
  const res = await request.settingPassword(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 刷新token /refresh/token
export async function refreshToken(options) {
  const { dispatch } = store
  const res = await request.refreshToken(options)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setToken,
      data: res.data.token,
    });
  })
}

// 商品分类分页查询 /category/list
export async function categoryList(options) {
  const { dispatch } = store
  const res = await request.categoryList(options)
  console.log(res)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setCategoryList,
      data: res.data
    })
  })
}

// 上传文件 /upload
export async function upload(options) {
  const { dispatch } = store
  console.log(options)
  const res = await request.upload(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 代理省市
export async function agentRegion(options) {
  const { dispatch } = store
  const res = await request.agentRegion(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 省市区县/乡 /region
export async function region(options) {
  const { dispatch } = store
  const res = await request.region(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 新增收货地址 /delivery/address/insert
export async function deliveryAddressInsert(options) {
  const { dispatch } = store
  const res = await request.deliveryAddressInsert(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 收货地址列表 /delivery/address/list
export async function deliveryAddressList(options) {
  const { dispatch } = store
  const res = await request.deliveryAddressList(options)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setAddressList,
      data: res.data
    })
  })
}

// 获取默认收货地址 /delivery/address/default
export async function deliveryAddressDefault(options) {
  const { dispatch } = store
  const res = await request.deliveryAddressDefault(options)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setDefaultAddres,
      data: res.data
    })
  })
}

// 通过id获取收货地址 /delivery/address/get
export async function deliveryAddressGet(options) {
  const { dispatch } = store
  const res = await request.deliveryAddressGet(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 通过ID删除收货地址 /delivery/address/del
export async function deliveryAddressDel(options) {
  const { dispatch } = store
  const res = await request.deliveryAddressDel(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 修改收货地址 /delivery/address/update
export async function deliveryAddressUpdate(options) {
  const { dispatch } = store
  const res = await request.deliveryAddressUpdate(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 商品搜索 /search
export async function search(options) {
  // const { dispatch } = store
  const res = await request.search(options)
  return requstPromise(res)
}

// 用户搜索记录 /search/log
export async function searchAsync(options) {
  const { dispatch } = store
  const res = await request.searchAsync(options)
  return requstPromise(res)
}

// 删除搜索记录 /search/log/del
export async function searchLogDel(options) {
  const { dispatch } = store
  const res = await request.searchLogDel(options)
  return requstPromise(res)
}

// 热门搜索/推荐搜索词 /search/recommend
export async function searchRecommend(options) {
  const { dispatch } = store
  const res = await request.searchRecommend(options)
  return requstPromise(res)
}

// 加入购物车 /shop/cart/add
export async function shopCartAdd(options) {
  const { dispatch } = store
  const res = await request.shopCartAdd(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res, () => {
    Toast.success(res.message, 1)
  })
}

// 我的购物车商品列表 /shop/cart/list
export async function shopCartList(options) {
  const { dispatch } = store
  const res = await request.shopCartList(options)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setShopCartList,
      data: res.data
    })
    let shopCartCheckList = {}
    res.data.records.map(item => {
      shopCartCheckList[item.cartId] = false
    })
    dispatch({
      type: ActionTypes.setShopCartCheckList,
      data: shopCartCheckList
    })
  })
}

// 修改购物车商品数量或规格 /shop/cart/update
export async function shopCartUpdate(options) {
  const { dispatch } = store
  const res = await request.shopCartUpdate(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res, () => {
    Toast.success(res.message, 1)
  })
}

// 删除购物车中一个或多个商品 /shop/cart/del
export async function shopCartDel(options) {
  const { dispatch } = store
  const res = await request.shopCartDel(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 代理订单提交
export async function orderAgentSubmit(options) {
  const { dispatch } = store
  const res = await request.orderAgentSubmit(options)
  return requstPromise(res)
}

// 订单取消
export async function orderCancel(options) {
  const { dispatch } = store
  const res = await request.orderCancel(options)
  return requstPromise(res)
}


// 订单确认
export async function orderConfirm(options) {
  const { dispatch } = store
  const res = await request.orderConfirm(options)
  return requstPromise(res)
}

// 确认收货
export async function orderConfirmReceipt(options) {
  const { dispatch } = store
  const res = await request.orderConfirmReceipt(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res, () => {
    Toast.success(res.message, 1)
  })
}

// 删除订单
export async function orderDel(options) {
  const { dispatch } = store

  const res = await request.orderDel(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 订单详情
export async function orderDetail(options) {
  const { dispatch } = store
  const res = await request.orderDetail(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 我的订单列表
export async function orderList(options) {
  const { dispatch } = store
  const res = await request.orderList(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 查找物流
export async function logistics(options) {
  const { dispatch } = store
  const res = await request.logistics(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 订单提交
export async function orderSubmit(options) {
  const { dispatch } = store
  const res = await request.orderSubmit(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 订单退款
export async function orderRefund(options) {
  const { dispatch } = store
  const res = await request.orderRefund(options)
  // if (res) {
  //   dispatch({
  //     type: ActionTypes.GET_USER_INFO,
  //     data: res.data
  //   })
  // }
  return requstPromise(res)
}

// 账号设置 POST
export async function accountSetting(option) {
  const { dispatch } = store
  const res = await request.accountSetting(option)
  return requstPromise(res)
}

// 校验手机号码是否注册了账号 POST
export async function checkPhone(option, callback) {
  const { dispatch } = store
  const res = await request.checkPhone(option, callback)
  return requstPromise(res)
}

// 意见反馈 POST
export async function feedback(option) {
  const { dispatch } = store
  const res = await request.feedback(option)
  return requstPromise(res)
}

// 邀请接口 POST
export async function invitation(option) {
  const { dispatch } = store
  const res = await request.invitation(option)
  return requstPromise(res)
}

// 我的信息 POST
export async function my(option) {
  const { dispatch } = store
  const res = await request.my(option)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setUserInfo,
      data: res.data,
    })
  })
}

// 我的帖子列表 POST
export async function myCard(option) {
  const { dispatch } = store
  const res = await request.myCard(option)
  return requstPromise(res)
}

// 我的分身币 POST
export async function myCoin(option) {
  const { dispatch } = store
  const res = await request.myCoin(option)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setCoin,
      data: res.data,
    })
  })
}

// 我的分身币明细 POST
export async function myCoinLog(option) {
  const { dispatch } = store
  const res = await request.myCoinLog(option)
  return requstPromise(res)
}

// 我收藏的帖子 POST
export async function myCollectionCard(option) {
  const { dispatch } = store
  const res = await request.myCollectionCard(option)
  return requstPromise(res)
}

// 我的CV POST
export async function myCv(option) {
  const { dispatch } = store
  const res = await request.myCv(option)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setCv,
      data: res.data,
    })
  })
}

// 我的CV明细 POST
export async function myCvLog(option) {
  const { dispatch } = store
  const res = await request.myCvLog(option)
  return requstPromise(res)
}

// 我的粉丝 POST
export async function myFans(option) {
  const { dispatch } = store
  const res = await request.myFans(option)
  return requstPromise(res)
}

// 我关注的人 POST
export async function myFollowAccount(option) {
  const { dispatch } = store
  const res = await request.myFollowAccount(option)
  return requstPromise(res)
}

// 我关注的板块 POST
export async function myFollowPlate(option) {
  const { dispatch } = store
  const res = await request.myFollowPlate(option)
  return requstPromise(res)
}

// 我的信息-帖子相关 POST
export async function myInfo(option) {
  const { dispatch } = store
  const res = await request.myInfo(option)
  return requstPromise(res)
}

// 我的积分 POST
export async function myIntegral(option) {
  const { dispatch } = store
  const res = await request.myIntegral(option)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setIntegral,
      data: res.data,
    })
  })
}

// 我的积分明细 POST
export async function myIntegralLog(option) {
  const { dispatch } = store
  const res = await request.myIntegralLog(option)
  return requstPromise(res)
}

// 我点赞的帖子 POST
export async function myLikeCard(option) {
  const { dispatch } = store
  const res = await request.myLikeCard(option)
  return requstPromise(res)
}
// 我的团队 POST
export async function myTeam(option) {
  const { dispatch } = store
  const res = await request.myTeam(option)
  return requstPromise(res)
}

// 二维码 GET
export async function qrcodeAccountNo(option) {
  const { dispatch } = store
  const res = await request.qrcodeAccountNo(option)
  return requstPromise(res)
}

// // 二维码 HEAD
// export async function qrcodeAccountNo(option) {
//   const { dispatch } = store
//   const res = await request.qrcodeAccountNo(option)
//   return requstPromise(res)
// }

// // 二维码 POST
// export async function qrcodeAccountNo(option) {
//   const { dispatch } = store
//   const res = await request.qrcodeAccountNo(option)
//   return requstPromise(res)
// }

// // 二维码 PUT
// export async function qrcodeAccountNo(option) {
//   const { dispatch } = store
//   const res = await request.qrcodeAccountNo(option)
//   return requstPromise(res)
// }

// // 二维码 DELETE
// export async function qrcodeAccountNo(option) {
//   const { dispatch } = store
//   const res = await request.qrcodeAccountNo(option)
//   return requstPromise(res)
// }

// // 二维码 OPTIONS
// export async function qrcodeAccountNo(option) {
//   const { dispatch } = store
//   const res = await request.qrcodeAccountNo(option)
//   return requstPromise(res)
// }

// // 二维码 PATCH
// export async function qrcodeAccountNo(option) {
//   const { dispatch } = store
//   const res = await request.qrcodeAccountNo(option)
//   return requstPromise(res)
// }

// 修改密码 POST
export async function updatePassword(option) {
  const { dispatch } = store
  const res = await request.updatePassword(option)
  return requstPromise(res)
}

// 修改手机号码 POST
export async function updatePhone(option) {
  const { dispatch } = store
  const res = await request.updatePhone(option)
  return requstPromise(res)
}

// 分身币排行
export async function rankCoin(option) {
  const { dispatch } = store
  const res = await request.rankCoin(option)
  return requstPromise(res)
}

// 积分排行
export async function rankIntegral(option) {
  const { dispatch } = store
  const res = await request.rankIntegral(option)
  return requstPromise(res)
}

// CV排行
export async function rankCv(option) {
  const { dispatch } = store
  const res = await request.rankCv(option)
  return requstPromise(res)
}

// 代理商品列表
export async function agentGoodsList(option) {
  const { dispatch } = store
  const res = await request.agentGoodsList(option)
  return requstPromise(res)
}

// 代理产品列表
export async function agentProductList(option) {
  const { dispatch } = store
  const res = await request.agentProductList(option)
  return requstPromise(res)
}

// 请求海报列表
export async function billList(options) {
  const { dispatch } = store
  const res = await request.billList(options)
  if (res) {
    dispatch({
      type: ActionTypes.setBillList,
      data: res.data
    })
  }
  return requstPromise(res)
}

// 请求海报详情
export async function billDetail(options) {
  const { dispatch } = store
  const res = await request.billDetail(options)
  return requstPromise(res)
}

// 请求海报
export async function touristBill(options) {
  const { dispatch } = store
  return await request.touristBill(options)
}

// 生成商品的分享海报
export async function touristBillGood(options) {
  const { dispatch } = store
  return await request.touristBillGood(options)
}



// 添加发票
export async function invoiceAdd(options) {
  const { dispatch } = store
  return await request.invoiceAdd(options)
}

// 获取默认发票
export async function invoiceDefault(options) {
  const { dispatch } = store
  return await request.invoiceDefault(options)
}

// 删除发票
export async function invoiceDel(options) {
  const { dispatch } = store
  return await request.invoiceDel(options)
}

// 编辑发票
export async function invoiceEdit(options) {
  const { dispatch } = store
  return await request.invoiceEdit(options)
}

// 获取发票
export async function invoiceGet(options) {
  const { dispatch } = store
  return await request.invoiceGet(options)
}

// 发票列表
export async function invoiceList(options) {
  const { dispatch } = store
  const res = await request.invoiceList(options)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setInvoicesList,
      data: res.data
    })
  })
}

// 订单支付
export async function paymentOrder(option) {
  const { dispatch } = store
  const res = await request.paymentOrder(option)
  return requstPromise(res, () => {

  })
}

// 获取支付方式
export async function paymentWay(option) {
  const { dispatch } = store
  const res = await request.paymentWay(option)
  return requstPromise(res, () => {

  })
}


// 账号关联请求审核
export async function relationAccountExamine(option) {
  const { dispatch } = store
  const res = await request.relationAccountExamine(option)
  return requstPromise(res, () => {

  })
}

// 我的关联账号列表
export async function relationAccountList(option) {
  const { dispatch } = store
  const res = await request.relationAccountList(option)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setAccountList,
      data: res.data
    })
  })
}

// 账号关联请求
export async function relationRequest(option) {
  const { dispatch } = store
  const res = await request.relationRequest(option)
  return requstPromise(res, () => {

  })
}

// 我的账号关联请求列表
export async function relationRequestList(option) {
  const { dispatch } = store
  const res = await request.relationRequestList(option)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setRequestList,
      data: res.data
    })
  })
}

// 收藏/取消收藏 帖子
export async function cardCollection(option) {
  const { dispatch } = store
  const res = await request.cardCollection(option)
  return requstPromise(res, () => {

  })
}

// 发表评论
export async function cardComment(option) {
  const { dispatch } = store
  const res = await request.cardComment(option)
  return requstPromise(res, () => {

  })
}

// 删除 评论/回复
export async function cardCommentDel(option) {
  const { dispatch } = store
  const res = await request.cardCommentDel(option)
  return requstPromise(res, () => {

  })
}

// 评论/回复 列表
export async function cardCommentList(option) {
  const { dispatch } = store
  const res = await request.cardCommentList(option)
  return requstPromise(res, () => {

  })
}

// 删除帖子
export async function cardDel(option) {
  const { dispatch } = store
  const res = await request.cardDel(option)
  return requstPromise(res, () => {

  })
}

// 帖子详情
export async function cardDetail(option) {
  const { dispatch } = store
  const res = await request.cardDetail(option)
  return requstPromise(res, () => {

  })
}

// 商品帖子列表
export async function cardGoodsList(option) {
  const { dispatch } = store
  const res = await request.cardGoodsList(option)
  return requstPromise(res, () => {

  })
}

// 关注/取消关注 帖子发布人
export async function cardFollowAccount(option) {
  const { dispatch } = store
  const res = await request.cardFollowAccount(option)
  return requstPromise(res, () => {

  })
}

// 点赞/取消点赞 帖子
export async function cardLike(option) {
  const { dispatch } = store
  const res = await request.cardLike(option)
  return requstPromise(res, () => {

  })
}

// 板块帖子列表
export async function cardList(option) {
  const { dispatch } = store
  const res = await request.cardList(option)
  return requstPromise(res, () => {

  })
}

// 我发布帖子列表
export async function cardMyList(option) {
  const { dispatch } = store
  const res = await request.cardMyList(option)
  return requstPromise(res, () => {

  })
}

// 保存帖子
export async function cardSave(option) {
  const { dispatch } = store
  const res = await request.cardSave(option)
  return requstPromise(res, () => {
    Toast.success(res.message, 1)
  })
}

// 板块详情
export async function plateDetail(option) {
  const { dispatch } = store
  const res = await request.plateDetail(option)
  return requstPromise(res, () => {

  })
}

// 关注/取消 板块
export async function plateFollow(option) {
  const { dispatch } = store
  const res = await request.plateFollow(option)
  return requstPromise(res, () => {

  })
}

// 热门板块列表
export async function requestPlateHotList(option) {
  const { dispatch } = store
  const res = await request.plateHotList(option)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setPlateHotList,
      data: res.data
    })
  })
}

// 板块列表
export async function requestPlateList(option) {
  const { dispatch } = store
  const res = await request.plateList(option)
  return requstPromise(res, () => {
    dispatch({
      type: ActionTypes.setPlateList,
      data: res.data
    })
  })
}

// 保存心率信息
export async function deviceHeartRate(option) {
  const { dispatch } = store
  const res = await request.deviceHeartRate(option)
  return requstPromise(res, () => {
    Toast.success(res.message, 1)
  })
}

// 心率信息详情
export async function deviceHeartRateDetail(option) {
  const { dispatch } = store
  const res = await request.deviceHeartRateDetail(option)
  return requstPromise(res, () => {

  })
}

// 心率信息列表
export async function deviceHeartRateList(option) {
  const { dispatch } = store
  const res = await request.deviceHeartRateList(option)
  return requstPromise(res, () => {

  })
}

// 提现
export async function withdrawal(option) {
  const { dispatch } = store
  const res = await request.withdrawal(option)
  return requstPromise(res, () => {

  })
}
